﻿var CalculatorDvk = function (ddlLocationTypeDvk, ddlNationalDvk, ddlCityDvk
                            , ddlLocationDvk, ddlPartnerDvk, ddlServiceDvk
                            , dteBeginDateDvk, txtPriceAdultsDvk, txtPriceChildsDvk
                            , txtQuantityAdultsDvk, txtQuantityChildDvk, txtVatDvk
                            , txtTotalPriceDvk, txtNoteDvk, seasonTour, tourInfo 
                            , txtBabyChildsDvk, txtQuantityBabyDvk, txtPriceDoanDvk
                            , txtQuantityAdults,txtQuantityChild,ddlServiceType,userIdElement) {
    $(txtPriceAdultsDvk).val(0);
    $(txtPriceChildsDvk).val(0);
    $(txtQuantityChildDvk).val(0);
    $(txtQuantityAdultsDvk).val(0);
    $(txtTotalPriceDvk).val(0);
    $(txtPriceDoanDvk).val(0);
    $(txtVatDvk).val(0);
    $(txtBabyChildsDvk).val(0);
    $(txtQuantityBabyDvk).val(0);

    $('.PriceDoanService').hide();
    $('.PriceLeService').hide();

    
    //load thanh pho
    var loadCityService = function (CityId,LocationId, ServiceId, CityName,LocationName, ServiceName) {       
        var nationalId = $(ddlNationalDvk).val();
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "ListCityandConcernByNational", "NationalId": nationalId },
            dataType: "json",
            success: function (data) {
                $(ddlCityDvk).html('<option value="-1"> ---Chọn tỉnh/thành phố--- </option>');
                if (data && data.isError == false) {
                    $.each(data.arrayCity, function (index, item) {
                        $(ddlCityDvk)
                            .append($("<option></option>")
                                .attr("value", item.IDCity)
                                .text(item.NameCity));

                    });
                    $(ddlCityDvk).combobox('setValue', CityName);
                    $(ddlCityDvk).val(CityId)
                    loadLocationByLocationType( LocationId, ServiceId, LocationName, ServiceName)
                }
            }
        });        
    };
    $(ddlNationalDvk).combobox({
        select: function (event, ui) {
            loadCityService();
        }
    });

    // load dia diem 
    var loadLocationByLocationType = function (LocationId, ServiceId, LocationName, ServiceName) {
        var locationType = $(ddlLocationTypeDvk).val();
        var city = $(ddlCityDvk).val();
            $.ajax({
                type: "POST",
                url: "/ajax.html",
                data: { "function": "ListLocationByLocationType", "LocationType": locationType, 'city': city },
                dataType: "json",
                success: function (data) {
                    $(ddlLocationDvk).html('<option value="-1"> ---Chọn địa điểm--- </option>');
                    if (data && data.isError == false) {
                        $.each(data.array, function (index, item) {
                            $(ddlLocationDvk)
                                .append($("<option></option>")
                                    .attr("value", item.ID)
                                    .text(item.Name));
                        });
                        $(ddlLocationDvk).combobox('setValue', LocationName);
                        $(ddlLocationDvk).val(LocationId);
                        loadByServiceDvk(ServiceId,  ServiceName);
                    }
                }
            });     
    };
    $(ddlLocationTypeDvk).combobox({
        select: function (event, ui) {
            loadLocationByLocationType();
        }
    });
    $(ddlCityDvk).combobox({
        select: function (event, ui) {
            loadLocationByLocationType();
        }
    });
    //load dich vu
    var loadByServiceDvk = function (ServiceId, ServiceName) {
        var typeId = 8;
        var partnerId = $(ddlPartnerDvk).val();
        var locationId = $(ddlLocationDvk).val();
        var season = seasonTour;
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "PriceListByDvk", "TypeId": typeId, "PartnerId": partnerId, "LocationId": locationId, "SeasonTour": season },
            dataType: "json",
            success: function (data) {
                $(ddlServiceDvk).html('<option value="-1">--- Chọn dịch vụ ---</option>');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $(ddlServiceDvk)
                            .append($("<option></option>")
                                .attr("value", item.PriceId)
                                .text(item.ServiceName));
                    });
                    $(ddlServiceDvk).combobox('setValue', ServiceName);
                    $(ddlServiceDvk).val(ServiceId);
                }
            }
        });
    };
    $(ddlPartnerDvk).combobox({
        select: function (event, ui) {
            loadByServiceDvk();
        }
    });
    $(ddlLocationDvk).combobox({
        select: function (event, ui) {
            loadByServiceDvk();
        }
    });

    //load gias dvk
    var loadPricelDvk = function () {
        var serviceId = $(ddlServiceDvk).val();
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "PriceDvk", "ServiceId": serviceId },
            dataType: "json",
            success: function (data) { 
                if (data && data.isError == false) {
                    $(txtPriceAdultsDvk).val(data.PriceAdult);
                    $(txtPriceChildsDvk).val(data.PriceChild);
                    $(txtBabyChildsDvk).val(data.PriceBaby);
                    funTongDvk();
                }
            }

        });
    };
    var funTongDvk = function () {
        var priceAdults = $(txtPriceAdultsDvk).val();
        var priceChilds = $(txtPriceChildsDvk).val();
        var quantityAdults = $(txtQuantityAdultsDvk).val();
        var quantityChild = $(txtQuantityChildDvk).val();
        var priceDoan = $(txtPriceDoanDvk).val();
        var babyChilds  = $(txtBabyChildsDvk).val();
        var quantityBaby  = $(txtQuantityBabyDvk).val();
    
        var vat = $(txtVatDvk).val();
        var total = 0;
        
        total = ((parseInt(priceDoan) * parseInt(quantityAdults)) + (parseInt(priceAdults) * parseInt(quantityAdults)) + (parseInt(priceChilds) * parseInt(quantityChild)) + (parseInt(babyChilds) * parseInt(quantityBaby)));
            
        
        $(txtTotalPriceDvk).val(total);
        $(txtVatDvk).val(total*0.1);
    }; 
    // goi load gia
    $(ddlServiceDvk).combobox({
        select: function (event, ui) {
            loadPricelDvk();
        }
    });
    $(txtPriceAdultsDvk).keyup(funTongDvk);
    $(txtPriceChildsDvk).keyup(funTongDvk);
    $(txtQuantityAdultsDvk).keyup(funTongDvk);
    $(txtQuantityChildDvk).keyup(funTongDvk);
    $(txtPriceDoanDvk).keyup(funTongDvk);
    $(txtBabyChildsDvk).keyup(funTongDvk);
    $(txtQuantityBabyDvk).keyup(funTongDvk);
    
    $(ddlServiceType).change(function () {
        var serviceType = $(ddlServiceType).val();

        if (serviceType == '1') {
            $('.PriceDoanService').hide();
            $('.PriceLeService').show();
            $(txtPriceDoanDvk).val(0);
            $(txtQuantityAdultsDvk).val(0);
        } else {
            $('.PriceLeService').hide();
            $('.PriceDoanService').show();
            $(txtPriceAdultsDvk).val(0);
            $(txtPriceChildsDvk).val(0);
            $(txtBabyChildsDvk).val(0);
            $(txtQuantityAdultsDvk).val(0);
            $(txtQuantityChildDvk).val(0);
            $(txtQuantityBabyDvk).val(0);
        }
        funTongDvk();
        loadPricelDvk();
    });
    
    //ham them sua xoa
    var fncService= function () {

        var typeId = 8;
        var tourId = parseInt(tourInfo);
        var dateMenu = $(dteBeginDateDvk).val();
        var menuElementId = $(ddlServiceDvk).val();
        var priceDoan = $(txtPriceDoanDvk).val();
        var priceAdults = $(txtPriceAdultsDvk).val();
        var priceChilds = $(txtPriceChildsDvk).val();
        var priceBaby = $(txtBabyChildsDvk).val();
        var quantityAdults = $(txtQuantityAdultsDvk).val();
        var quantityChild = $(txtQuantityChildDvk).val();
        var quantityBaby = $(txtQuantityBabyDvk).val();
        
        var vat = $(txtVatDvk).val();
        if (vat > 0 ) { } else { vat = 0; }
        var note = $(txtNoteDvk).val();
        
        var quantityAdultsTour = parseInt(txtQuantityAdults);
        var quantityChildTour = parseInt(txtQuantityChild);
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        var concern = $(ddlPartnerDvk).val();
        var userId = parseInt(userIdElement);


        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: {
                "function": "InsertDvk", "TypeId": typeId, "TourId": tourId, "menuElementID": menuElementId,
                "PriceAdults": priceAdults, "PriceChilds": priceChilds, "Date": dateMenu,
                "CalculatorId": calculatorId, "ClassName": className, 'Vat': vat,
                'QuantityAdults': quantityAdults, 'QuantityChild': quantityChild,
                'QuantityBaby': quantityBaby, 'PriceBaby': priceBaby, 'Note': note,
                'PriceDoan': priceDoan, 'QuantityAdultsTour': quantityAdultsTour, 'QuantityChildTour': quantityChildTour,
                'Concern': concern, 'UserId': userId
            },
            dataType: "json",
            success: function (data) {

                $(".ListDvk").remove();
                $(ddlLocationTypeDvk).val('-1');
                $(ddlNationalDvk).val('-1');
                $(ddlCityDvk).val('-1');
                $(ddlLocationDvk).val('-1');
                $(ddlPartnerDvk).val('-1');
                $(ddlServiceDvk).val('-1');
                $(ddlServiceType).val('-1');
                $(dteBeginDateDvk).val('');
                $(txtNoteDvk).val('');
                $(txtTotalPriceDvk).val('0');
                $(txtVatDvk).val('0');
                $(txtQuantityChildDvk).val('0');
                $(txtQuantityAdultsDvk).val('0');
                $(txtPriceAdultsDvk).val('0');
                $(txtPriceDoanDvk).val('0');
                $(txtPriceChildsDvk).val('0');
                $(txtBabyChildsDvk).val('0');
                $(txtQuantityBabyDvk).val('0');
                $(txtNoteDvk).val('');
                //, txtBabyChildsDVK ,  txtQuantityBabyDVK
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        
                        $('#new-Dvk')
                            .before($("<tr class='ListDvk' id='Dvk-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                             
                                '<td >' + item.LocationName + '</td>' +
                                '<td >' + item.PartnerName + '</td>' +
                                '<td >' + item.ServiceName + '</td>' +
                                '<td >' + item.DateBegin + '</td>' +
                                '<td >' + item.PriceDoan + '</td>' +
                                '<td >' + item.PriceAdults + '</td>' +
                                '<td >' + item.PriceChild + '</td>' +
                                '<td >' + item.PriceBaby + '</td>' +
                                '<td >' + item.QuantityAdults + '</td>' +
                                '<td >' + item.QuantityChild + '</td>' +
                                '<td >' + item.QuantityBaby + '</td>' +
                                
                                '<td >' + item.TotalPrice + '</td>' +
                                '<td >' + item.Vat + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateDvk" onclick="openPopup(\'ticket-service\')"  type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteDvk"  type="button" value="" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));
                    });
                    $('#TotalDvkPrices').html(parseInt(data.TotalDvkChild) + parseInt(data.TotalDvkAdults)+ parseInt(data.TotalDvkAdults));
                    $('#TotalDvkAdults').html(data.TotalDvkAdults);
                    $('#TotalDvkChild').html(data.TotalDvkChild);
                    $('#TotalVatService').html(data.TotalVatService);
                    $('#btnInsertDvk').attr("calculatorid", "-1").val("Thêm");
                    hidePopup('ticket-service');
                    TotalPrice();
                    fncTotalCalculator();
                }
            }
        });
        
    };
    $('#btnInsertDvk').on("click", fncService);
    $('#btnDeleteDvk').on("click", fncService);
    $('table').on("click", 'tr >td >input.btnDeleteDvk', fncService);
    $('table').on("click", 'tr >td >input.btnUpdateDvk', function () {
        var calculatorId = $(this).attr("calculatorid"); 
        $.ajax({
            type: "POST",
            url: "/ajax_hc.html",
            data: { "function": "UpdateDvk", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
               
                $('#Dvk-' + calculatorId).remove();
                $(ddlLocationTypeDvk).combobox('setValue', data.LocationTypeName);
                $(ddlLocationTypeDvk).val(data.LocationTypeId);
                $(ddlNationalDvk).combobox('setValue', data.NationalName);
                $(ddlNationalDvk).val(data.NationalId);
                $(ddlPartnerDvk).combobox('setValue', data.PartnerName);
                $(ddlPartnerDvk).val(data.PartnerId);
                loadCityService( data.CityId, data.LocationId, data.ServiceId, data.CityName, data.LocationName, data.ServiceName);
              
                $(dteBeginDateDvk).val(data.DateBegin);
                $(txtTotalPriceDvk).val(data.TotalPrice);
                $(txtVatDvk).val(data.Vat);
                $(txtPriceDoanDvk).val(data.PriceDoan);
                if (data.PriceDoan == "0") {
                    $(ddlServiceType).val('1');
                } else {
                    $(ddlServiceType).val('2');
                }
                $(txtQuantityChildDvk).val(data.QuantityChild);
                $(txtQuantityAdultsDvk).val(data.QuantityAdults);
                $(txtPriceAdultsDvk).val(data.PriceAdults); 
                $(txtPriceChildsDvk).val(data.PriceChild);
                $(txtQuantityBabyDvk).val(data.QuantityBaby);
                $(txtBabyChildsDvk).val(data.PriceBaby);
                $(txtNoteDvk).val(data.Note);
                
                $('#btnInsertDvk').attr("calculatorid", calculatorId).val("Lưu");
                
            }
        });
    });
   

    //var loadCityByService = function (nationalId, partnerId, cityId, provinceElement, partnerElement, locationTypeId, ddlLocationTypeDvk, LocationId,  serviceId) {
      
    //    $.ajax({
    //        type: "POST",
    //        url: "/ajax_hc.html",
    //        data: { "function": "ListCityandConcernByNational", "NationalId": nationalId },
    //        dataType: "json",
    //        success: function (data) {
    //            $(provinceElement).html('<option value="-1"> --- Chọn tỉnh/thành phố--- </option>');
    //            $(partnerElement).html('<option value="-1"> --- Chọn nhà cung cấp--- </option>');
    //            if (data && data.isError == false) {
    //                $.each(data.arrayCity, function (index, item) {
    //                    $(provinceElement)
    //                        .append($("<option></option>")
    //                            .attr("value", item.IDCity)
    //                            .text(item.NameCity));
    //                });

    //                $.each(data.arrayPartner, function (index, item) {
    //                    $(partnerElement)
    //                        .append($("<option></option>")
    //                            .attr("value", item.IDPartner)
    //                            .text(item.NamePartner));
    //                });
    //            }
    //            $(ddlCityDvk).val(cityId);
    //            $(ddlPartnerDvk).val(partnerId);
    //            loadLocationByType(locationTypeId, ddlLocationTypeDvk, LocationId,    serviceId);
    //        }
    //    });



    //};
    //var loadLocationByType = function (locationTypeId, ddlLocationTypeDvk, LocationId,     serviceId) {
     
    //    $.ajax({
    //        type: "POST",
    //        url: "/ajax_hc.html",
    //        data: { "function": "ListLocationByLocationType", "LocationType": locationTypeId },
    //        dataType: "json",
    //        success: function (data) {
    //            $(ddlLocationDvk).html('<option value="-1"> ---Chọn địa điểm--- </option>');
    //            if (data && data.isError == false) {
    //                $.each(data.array, function (index, item) {
    //                    $(ddlLocationDvk)
    //                        .append($("<option></option>")
    //                            .attr("value", item.ID)
    //                            .text(item.Name));
    //                });
    //            }
    //            $(ddlLocationDvk).val(LocationId);
    //            loadByService(   serviceId);
    //        }
    //    });
    //};
    //var loadByService = function (  serviceId) {
      
    //    var typeId = 8;
    //    var partnerId = $(ddlPartnerDvk).val();
    //    var locationId = $(ddlLocationDvk).val();
    //    var season = seasonTour;
    //    $.ajax({
    //        type: "POST",
    //        url: "/ajax_hc.html",
    //        data: { "function": "PriceListByDvk", "TypeId": typeId, "PartnerId": partnerId, "LocationId": locationId, "SeasonTour": season },
    //        dataType: "json",
    //        success: function (data) {
    //            $(ddlServiceDvk).html('<option value="-1">--- Chọn dịch vụ ---</option>');
    //            if (data && data.isError == false) {
    //                $.each(data.array, function (index, item) {
    //                    $(ddlServiceDvk)
    //                        .append($("<option></option>")
    //                            .attr("value", item.PriceId)
    //                            .text(item.ServiceName));
    //                });
    //                $(ddlServiceDvk).val(serviceId);
    //            }
               
    //        }
    //    });
    //};
};

